package singlelist;

import listinterface.IList;
import myexception.ListIndexOutOfBoundsException;

/**
 * @Author 12629
 * @Description：
 */
public class MySingleList implements IList {

    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

        public int getVal() {
            return val;
        }
    }

    public ListNode head; //表示当前链表的头节点

    public int listSize;

    //手动创建一个链表
    public void createList() {

        ListNode node1 = new ListNode(12);

        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }



    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
        listSize++;
        /*if(head == null) {
            head = node;
        }else {
            //对于插入来说 我们一般建议 先绑定后边
            node.next = head;
            head = node;
        }*/
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        //表示链表为空
        if(head == null) {
            head = node;
            listSize++;
            return;
        }
        //找到链表的尾巴
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
        listSize++;
    }

    @Override
    public void addIndex(int index, int data)
            throws ListIndexOutOfBoundsException {

        checkIndexOfAdd(index);

        ListNode node = new ListNode(data);

        if(index == 0) {
            addFirst(data);
            return;
        }

        if(index == size()) {
            addLast(data);
            return;
        }

        ListNode cur = findIndex(index);

        node.next = cur.next;

        cur.next = node;

        listSize++;
    }

    private void checkIndexOfAdd(int index) {
        if(index < 0 || index > size()) {
            throw new ListIndexOutOfBoundsException("插入index位置不合法,index = "+index);
        }
    }

    /**
     * 找到index-1位置节点的地址
     * @param index
     * @return
     */
    private ListNode findIndex(int index) {
        ListNode cur = head;
        int count = 0;
        while (count != index-1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }

        return false;
    }

    @Override
    public void remove(int key) {
        if(head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
            listSize--;
            return;
        }
        ListNode cur = findNode(key);
        if(cur == null) {
            System.out.println("没有你要删除的数据");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
        listSize--;
    }
    //找到第一个遇到的key的前一个节点
    //没有 找到 返回null
    private ListNode findNode(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        /*while(head.val == key) {
            head = head.next;
        } 有可能会出现空指针异常*/
        ListNode cur = head.next;
        ListNode prev = head;

        while (cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                //cur = cur.next;
                listSize--;
            }else {
                prev = cur;
                //cur = cur.next;
            }
            cur = cur.next;
        }
        if(head.val == key) {
            head = head.next;
            listSize--;
        }
    }

    @Override
    public int size() {
       /* ListNode cur = head;
        int usedSize = 0;
        while (cur != null) {
            usedSize++;
            cur = cur.next;
        }
        return usedSize;*/
        return listSize;
    }

    @Override
    public void clear() {
        head = null;
        listSize = 0;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //从指定节点 开始进行打印
    public void display(ListNode newHead) {
        ListNode cur = newHead;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public ListNode reverseList() {
        if(head == null) {
            return null;
        }
        //只有一个节点
        if(head.next == null) {
            return head;
        }

        ListNode cur = head.next;
        head.next = null;

        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = head;
            head = cur;
            cur = curN;
        }
        return head;
    }

    /**
     * 链表的中间节点
     * 方法一
     * @return
     */
    public ListNode middleNode() {
        if(head == null) {
            return head;
        }
        int len = size();
        int index = len / 2;
        ListNode cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    public ListNode middleNode(ListNode head) {
        if(head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
        //while (fast.next != null && fast != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public int kthToLast(int k) {
        if(k <= 0 /*|| k > len*/ ) {
            return -1;
        }
        ListNode fast = head;
        ListNode slow = head;

        int count = 0;
        //
        while (count != k-1) {
            fast = fast.next;
            if(fast == null) {
                return -1;
            }
            count++;
        }
        //同时走
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }

    public ListNode partition( int x) {
        // write code here
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        ListNode cur = head;
        while (cur != null) {
            if(cur.val < x) {
                //第一次插入
                if(bs == null) {
                    bs = be = cur;
                }else {
                    be.next = cur;
                    be = cur;
                }
            }else {
                //第一次插入
                if(as == null) {
                    as = ae = cur;
                }else {
                    ae.next = cur;
                    ae = cur;
                }
            }
            cur = cur.next;
        }
        if(bs == null) {
            return as;
        }
        be.next = as;
        if(as != null) {
            ae.next = null;
        }
        return bs;
    }

    public boolean chkPalindrome() {
        if(head == null) {
            return false;
        }
        if(head.next == null) {
            return true;
        }
        //1. 找中间节点的位置
        ListNode fast = head;
        ListNode slow = head;

        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //2.开始翻转 后半部分
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curN;
        }
        //3.一个从头开始走  一个从尾开始走
        while (head != slow) {
            if(head.val != slow.val) {
                return false;
            }
             //判断偶数情况
            if(head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    public boolean hasCycle() {

        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                return true;
            }
        }
        return false;
    }

    public void createLoop() {
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = head.next.next.next;
    }

    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                break;
            }
        }
        if(fast == null || fast.next == null) {
            return null;//没有环 就没有入口点
        }

        fast = head;
        while(fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}
